<template>
  <div>
    <!-- form 表单 -->
    <van-form
      @submit="onSubmit"
      class="m-15"
      label-width="40px"
      input-align="right"
    >
      <van-cell-group inset>
        <van-field
          v-model="username"
          name="昵称"
          label="昵称"
          placeholder="请填写昵称"
          maxlength="20"
          clearable
          :rules="[{ required: true, message: '' }]"
        />
        <van-field
          v-model="nickname"
          name="姓名"
          label="姓名"
          clearable
          placeholder="请填写姓名"
          maxlength="10"
          :rules="[{ required: true, message: '' }]"
        >
          <template #button>
            <span @click="actionShow = true">
              先生
              <van-icon :name="actionShow ? 'arrow-up' : 'arrow-down'" />
            </span>
          </template>
        </van-field>
        <van-field name="radio" label="性别">
          <template #input>
            <van-radio-group v-model="checked" direction="horizontal">
              <van-radio name="1">男</van-radio>
              <van-radio name="0">女</van-radio>
            </van-radio-group>
          </template>
        </van-field>
        <van-field
          v-model="phone"
          center
          clearable
          label="手机"
          type="tel"
          placeholder="请输入手机号码"
          :rules="[{ required: true, message: '' }]"
        >
          <template #button>
            <van-button
              size="mini"
              type="default"
              class="ml-10 pl-10 pr-10"
              @click="dialogShow = true"
              >更换</van-button
            >
          </template>
        </van-field>
        <van-field
          v-model="result"
          readonly
          name="datetimePicker"
          label="生日"
          placeholder="请选择破蛋日"
          @click="birthDay ? '' : (showPicker = true)"
          :disabled="birthDay"
          :rules="[{ required: true, message: '' }]"
        />
        <van-popup v-model:show="showPicker" position="bottom">
          <van-datetime-picker
            type="date"
            title="请选择破蛋日"
            :min-date="minDate"
            :max-date="maxDate"
            @confirm="onConfirm"
            @cancel="showPicker = false"
          />
        </van-popup>
      </van-cell-group>
      <div class="mt-15">
        <van-button block type="primary" native-type="submit" size="small" color="linear-gradient(to right, #ff6034, #ee0a24)">
          点击完善信息
        </van-button>
      </div>
    </van-form>
    <!-- 对话框 -->
    <edit-phoe-dialog v-model:dialogShow="dialogShow" @submit-phone="onSubmitPhoe"></edit-phoe-dialog>
    <!-- 动作框 -->
    <van-action-sheet
      description="请选择对您的称谓"
      v-model:show="actionShow"
      :actions="actions"
      @select="onSelect"
      cancel-text="取消"
    />
  </div>
</template>

<script>
import { reactive, toRefs } from "vue";
import days from "../../../utils/days";
import editPhoeDialog from "./editPhoeDialog.vue";
export default {
  components: {
    editPhoeDialog
  },
  setup() {
    const state = reactive({
      username: "",
      nickname: "",
      actions: [{ name: "先生" }, { name: "女士" }],
      actionShow: false,
      checked: "",
      phone: "",
      dialogShow: false,
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 10, 1),
      result: "",
      birthDay: false,
      showPicker: false,
    });
    if (state.result) state.birthDay = true;
    const onSelect = (item) => {
      state.actionShow = false;
      console.log(item.name);
    };
    const onConfirm = (value) => {
      state.result = days(value).yymmdd;
      state.showPicker = false;
    };
    const onSubmit = (values) => {
      console.log("submit", values);
    };
    const onSubmitPhoe = (values) => {
      console.log("onSubmitPhoe", values);
      state.dialogShow=false
    };
    return {
      ...toRefs(state),
      onConfirm,
      onSubmitPhoe,
      onSubmit,
      onSelect,
    };
  },
};
</script>

<style lang="less" scoped></style>
